using System;
using System.Data;
using System.Data.OleDb;
using PCSComUtils.Common;
using PCSComUtils.DataAccess;

using PCSComUtils.PCSExc;

namespace PCSComPayroll.DS
{
	public class PAY_AdditionSalaryDetailDS
	{
		public PAY_AdditionSalaryDetailDS()
		{
		}

		private const string THIS = "PCSComPayroll.DS.DS.PAY_AdditionSalaryDetailDS";

		public void Add(object pobjObjectVO)
		{
			const string METHOD_NAME = THIS + ".Add()";

			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				PAY_AdditionSalaryDetailVO objObject = (PAY_AdditionSalaryDetailVO) pobjObjectVO;
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);

				strSql = "INSERT INTO PAY_AdditionSalaryDetail("
					+ PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.LINE_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD + ","
					+ PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD + ","
					+ PAY_AdditionSalaryDetailTable.PRODUCTID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REASON_FLD + ")"
					+ "VALUES(?,?,?,?,?,?,?)";

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD].Value = objObject.AdditionSalaryMasterID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.LINE_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.LINE_FLD].Value = objObject.Line;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD].Value = objObject.RequestDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD].Value = objObject.RequestAmount;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD].Value = objObject.ApprovalAmount;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.REASON_FLD, OleDbType.WChar));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.REASON_FLD].Value = objObject.Reason;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		/// <summary>
		/// Delete by master id
		/// </summary>
		/// <param name="pintID"></param>
		public void Delete(int pintID)
		{
			const string METHOD_NAME = THIS + ".Delete()";
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = "DELETE " + PAY_AdditionSalaryDetailTable.TABLE_NAME
					+ " WHERE  " + PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD + "=" + pintID.ToString();
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();
				ocmdPCS = null;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}
			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public object GetObjectVO(int pintID)
		{
			const string METHOD_NAME = THIS + ".GetObjectVO()";
			DataSet dstPCS = new DataSet();

			OleDbDataReader odrPCS = null;
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;
				strSql = "SELECT "
					+ PAY_AdditionSalaryDetailTable.ADDITIONSALARYDETAILID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.PRODUCTID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.LINE_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD + ","
					+ PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REASON_FLD
					+ " FROM " + PAY_AdditionSalaryDetailTable.TABLE_NAME
					+ " WHERE " + PAY_AdditionSalaryDetailTable.ADDITIONSALARYDETAILID_FLD + "=" + pintID;

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				odrPCS = ocmdPCS.ExecuteReader();

				PAY_AdditionSalaryDetailVO objObject = new PAY_AdditionSalaryDetailVO();

				while (odrPCS.Read())
				{
					objObject.AdditionSalaryDetailID = int.Parse(odrPCS[PAY_AdditionSalaryDetailTable.ADDITIONSALARYDETAILID_FLD].ToString());
					objObject.AdditionSalaryMasterID = int.Parse(odrPCS[PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD].ToString());
					objObject.ProductID = int.Parse(odrPCS[PAY_AdditionSalaryDetailTable.PRODUCTID_FLD].ToString());
					objObject.Line = int.Parse(odrPCS[PAY_AdditionSalaryDetailTable.LINE_FLD].ToString());
					objObject.RequestDate = DateTime.Parse(odrPCS[PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD].ToString());
					objObject.RequestAmount = Decimal.Parse(odrPCS[PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD].ToString());
					objObject.ApprovalAmount = Decimal.Parse(odrPCS[PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD].ToString());
					objObject.Reason = odrPCS[PAY_AdditionSalaryDetailTable.REASON_FLD].ToString();
				}
				return objObject;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}
		}

		public void Update(object pobjObjecVO)
		{
			const string METHOD_NAME = THIS + ".Update()";

			PAY_AdditionSalaryDetailVO objObject = (PAY_AdditionSalaryDetailVO) pobjObjecVO;


			//prepare value for parameters
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = "UPDATE PAY_AdditionSalaryDetail SET "
					+ PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD + "=   ?" + ","
					+ PAY_AdditionSalaryDetailTable.PRODUCTID_FLD + "=   ?" + ","
					+ PAY_AdditionSalaryDetailTable.LINE_FLD + "=   ?" + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD + "=   ?" + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD + "=   ?" + ","
					+ PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD + "=   ?" + ","
					+ PAY_AdditionSalaryDetailTable.REASON_FLD + "=  ?"
					+ " WHERE " + PAY_AdditionSalaryDetailTable.ADDITIONSALARYDETAILID_FLD + "= ?";

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD].Value = objObject.AdditionSalaryMasterID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.LINE_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.LINE_FLD].Value = objObject.Line;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD].Value = objObject.RequestDate;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD].Value = objObject.RequestAmount;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD].Value = objObject.ApprovalAmount;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.REASON_FLD, OleDbType.WChar));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.REASON_FLD].Value = objObject.Reason;

				ocmdPCS.Parameters.Add(new OleDbParameter(PAY_AdditionSalaryDetailTable.ADDITIONSALARYDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PAY_AdditionSalaryDetailTable.ADDITIONSALARYDETAILID_FLD].Value = objObject.AdditionSalaryDetailID;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}

		}

		public DataSet List()
		{
			const string METHOD_NAME = THIS + ".List()";
			DataSet dstPCS = new DataSet();


			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;

				strSql = "SELECT "
					+ PAY_AdditionSalaryDetailTable.ADDITIONSALARYDETAILID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.PRODUCTID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.LINE_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD + ","
					+ PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REASON_FLD
					+ " FROM " + PAY_AdditionSalaryDetailTable.TABLE_NAME;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();

				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS, PAY_AdditionSalaryDetailTable.TABLE_NAME);

				return dstPCS;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}


		}

		public DataSet List(int pintMasterID)
		{
			const string METHOD_NAME = THIS + ".List()";
			DataSet dstPCS = new DataSet();
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try
			{
				string strSql = String.Empty;

				strSql = "SELECT "
					+ PAY_AdditionSalaryDetailTable.ADDITIONSALARYDETAILID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.LINE_FLD + ","
					+ "D." + PAY_AdditionSalaryDetailTable.PRODUCTID_FLD + ","
					+ "P." + ITM_ProductTable.CODE_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD + ","
					+ PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REASON_FLD
					+ " FROM " + PAY_AdditionSalaryDetailTable.TABLE_NAME + " D"
					+ " JOIN " + ITM_ProductTable.TABLE_NAME + " P"
					+ " ON D." + PAY_AdditionSalaryDetailTable.PRODUCTID_FLD + "= P." + ITM_ProductTable.PRODUCTID_FLD
					+ " WHERE " + PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD + "=" + pintMasterID;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();

				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS, PAY_AdditionSalaryDetailTable.TABLE_NAME);

				return dstPCS;
			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}


		}

		public void UpdateDataSet(DataSet pData)
		{
			const string METHOD_NAME = THIS + ".UpdateDataSet()";
			string strSql;
			OleDbConnection oconPCS = null;
			OleDbCommandBuilder odcbPCS;
			OleDbDataAdapter odadPCS = new OleDbDataAdapter();

			try
			{
				strSql = "SELECT "
					+ PAY_AdditionSalaryDetailTable.ADDITIONSALARYDETAILID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.ADDITIONSALARYMASTERID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.PRODUCTID_FLD + ","
					+ PAY_AdditionSalaryDetailTable.LINE_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTDATE_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REQUESTAMOUNT_FLD + ","
					+ PAY_AdditionSalaryDetailTable.APPROVALAMOUNT_FLD + ","
					+ PAY_AdditionSalaryDetailTable.REASON_FLD
					+ " FROM " + PAY_AdditionSalaryDetailTable.TABLE_NAME;


				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				odadPCS.SelectCommand = new OleDbCommand(strSql, oconPCS);
				odcbPCS = new OleDbCommandBuilder(odadPCS);
				pData.EnforceConstraints = false;
				odadPCS.Update(pData, PAY_AdditionSalaryDetailTable.TABLE_NAME);

			}
			catch (OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME, ex);
			}

			catch (InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);
			}
			catch (Exception ex)
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally
			{
				if (oconPCS != null)
				{
					if (oconPCS.State != ConnectionState.Closed)
					{
						oconPCS.Close();
					}
				}
			}

		}
	}
}